package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.ProductTechTrain;
import com.yqg.protection.service.IProductTechTrainService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ProductTechTrainController 前端控制器
 *
 * @author zxf
 * @since 2024-10-15
 */
@RestController
@RequestMapping("/product-tech-train")
public class ProductTechTrainController {

    @Resource
    private IProductTechTrainService productTechTrainService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者修改产品和技术培训")
    public Result save(@RequestBody ProductTechTrain productTechTrain) {
        productTechTrain.setIsDelete(0);
        return Result.success(productTechTrainService.saveOrUpdate(productTechTrain));
    }

    /**
     * 删除
     */
    @DeleteMapping("delete/{id}")
    @SysLog("删除产品和技术培训")
    public Result delete(@PathVariable Integer id) {
        ProductTechTrain productTechTrain = productTechTrainService.getById(id);
        productTechTrain.setIsDelete(1);
        return Result.success(productTechTrainService.saveOrUpdate(productTechTrain));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<ProductTechTrain> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<ProductTechTrain> list = productTechTrainService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(productTechTrainService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String keyWord) {
        QueryWrapper<ProductTechTrain> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.and(qw -> qw.like("train_name", keyWord).or().like("enter_name", keyWord));
        queryWrapper.orderByDesc("id");
        return Result.success(productTechTrainService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String keyWord) {
        productTechTrainService.export(response, keyWord);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入产品和技术培训")
    public void importData(MultipartFile file) {
        productTechTrainService.importData(file);
    }
}

